package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.Activity.AddOilActivity;
import com.tianxin.workbao.workBao.Activity.AddOilOauthActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Fueldiary;
import com.tianxin.workbao.workBao.dataProvide.AddOilProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class AddOilAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<Fueldiary> mFueldiarys;
    private Context mContext;

    public AddOilAdapter(List<Fueldiary> list, Context context) {
        this.mFueldiarys = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mFueldiarys.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fueldiary_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mFueldiarys.get(position));
            viewHolder.SetViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mFueldiarys.size() > 0) {
            return mFueldiarys.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView numberTV;
        TextView addTimeTV;
        TextView statusLabelTV;
        TextView quantityTV;
        TextView unitPriceTV;
        TextView totalMoneyTV;
        TextView oilNameTV;
        TextView carInformationTV;
        TextView addUserTV;
        LinearLayout click_view;
        ImageButton deleteBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            click_view = (LinearLayout) itemView.findViewById(R.id.click_view);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            addTimeTV = (TextView) itemView.findViewById(R.id.add_time);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            quantityTV = (TextView) itemView.findViewById(R.id.quantity);
            unitPriceTV = (TextView) itemView.findViewById(R.id.unit_price);
            totalMoneyTV = (TextView) itemView.findViewById(R.id.total_money);
            oilNameTV = (TextView) itemView.findViewById(R.id.oil_name);
            carInformationTV = (TextView) itemView.findViewById(R.id.car_information);
            addUserTV = (TextView) itemView.findViewById(R.id.add_user);
            deleteBtn = (ImageButton) itemView.findViewById(R.id.delete_btn);
        }

        public void BindViewData(Fueldiary fueldiary) {
            numberTV.setText(fueldiary.getNumber());
            quantityTV.setText(fueldiary.getOilmass() + "升");
            unitPriceTV.setText(fueldiary.getUnitprice() + "元/升");
            totalMoneyTV.setText(fueldiary.getTotalmoney() + "元");
            carInformationTV.setText(fueldiary.getPlatenumbers());
            if (fueldiary.getIsoauth() == 1) {
                deleteBtn.setVisibility(View.GONE);
                addUserTV.setText(fueldiary.getOauthusername());
                statusLabelTV.setText(mContext.getResources().getString(R.string.big_dot) + "已审");
                statusLabelTV.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
                if (fueldiary.getOauthtime() != null) {
                    addTimeTV.setText(TimeUtils.parseDateTime(fueldiary.getOauthtime()));
                }
            } else {
                deleteBtn.setVisibility(View.VISIBLE);
                addUserTV.setText(fueldiary.getAddusername());
                statusLabelTV.setText(mContext.getResources().getString(R.string.big_dot) + "未审");
                statusLabelTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_pressed_color));
                addTimeTV.setText(TimeUtils.parseDateTime(fueldiary.getAddtime()));
            }
        }

        public void SetViewListener(final int position) {
            click_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.click_view:
                            Fueldiary fueldiary = mFueldiarys.get(position);
                            if (fueldiary.getIsoauth() == 0) {
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("fueldiary", fueldiary);
                                Intent intent = new Intent(mContext, AddOilOauthActivity.class);
                                intent.putExtras(bundle);
                                mContext.startActivity(intent);
                            }
                            break;
                    }

                }
            });
            deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.delete_btn:
                            AddOilProvide provide = new AddOilProvide(mContext, new UpdateListViewListener() {
                                @Override
                                public void onRefreshData(Object result) {

                                }

                                @Override
                                public void onLoadMoreData(Object result) {

                                }

                                @Override
                                public void showMessage(String msg) {
                                    if (msg == "ok") {
                                        mFueldiarys.remove(position);
                                        notifyDataSetChanged();
                                        Toast.makeText(mContext, "操作成功", Toast.LENGTH_LONG).show();
                                    } else if (msg.contains("failure")) {
                                        Toast.makeText(mContext, "操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                                    }
                                }

                                @Override
                                public void showProgress() {
                                    AddOilActivity activity = (AddOilActivity) mContext;
                                    activity.getSweetAlertDialog(activity).showDialog("", 0);
                                }

                                @Override
                                public void hideProgress() {
                                    AddOilActivity activity = (AddOilActivity) mContext;
                                    activity.getSweetAlertDialog(activity).dismiss();
                                }
                            }, null);
                            provide.delete((int) mFueldiarys.get(position).getId());
                            break;
                    }
                }
            });
        }
    }
}
